import { ref, toRaw } from 'vue'
import { messageApi, contextHolder } from '@/utils/MessageUtils.js'
import service from '@/rpc/httpUtils.js'
import axios from 'axios'

// 根据id查询article
async function queryArticleById(articleId) {
  const res = await service.get(`/kc/queryArticleById/${articleId}`)
  return res
}

// 根据id删除article
async function deleteArticle(articleId) {
  const res = await service.get(`/kc/deleteArticle/${articleId}`)
  return res
}

// 查询article列表
async function queryArticleList(articleDto) {
  const res = await service.post(`/kc/queryArticleList`, articleDto)
  return res
}

// 查询article列表数量
async function queryArticleListTotal(articleDto) {
  const res = await service.post(`/kc/queryArticleListTotal`, articleDto)
  return res
}




// 查询字典
async function queryDict(classify) {
  const res = await service.get(`/kc/queryDict/${classify}`)
  return res
}

// 查询字典
async function queryArticleTags(kw) {
  const res = await service.post(`/kc/queryArticleTags`, {
    classify: 'TAG',
    title: kw
  })
  return res
}



// 新增/更新 article
async function saveOrUpdateArticle(articleDto) {
  const res = await service.post(`/kc/saveOrUpdateArticle`, articleDto)
  return res
}

// lucene索引全部文章
async function indexAllArticle() {
  const res = await service.get(`/kc/indexAllArticle`)
  return res
}

// 根据文章id对文章进行索引
async function indexArticleById(articleId) {
  const res = await service.get(`/kc/indexArticleById/${articleId}`)
  return res
}


// 根据附件id删除附件
async function deleteAttach(attachId) {
  const res = await service.get(`/kc/deleteAttach/${attachId}`)
  return res
}

// 根据附件id删除附件
async function queryAttachList(relaId, classify) {
  const res = await service.post(`/kc/queryAttachList`, {
    relaId,
    classify
  })
  return res
}

function uuid() {

  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
  });
  uuid = uuid.replace(/-/g, '');
  return uuid;

}





// 下载文件的核心函数
async function downloadFile(url, fileName) {
  const fileUrl = url; // 文件的URL地址
  fetch(fileUrl)
    .then(response => response.blob())
    .then(blob => {
      const url = window.URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', fileName);
      document.body.appendChild(link);
      link.click();
    })
    .catch(error => {
      console.error(error);
    });
}


function downloadAttach(id, fileName) {


  const cfg = import.meta.env;
  const baseURL = cfg.VITE_UPLOAD_BASE_URL;
  const url = `${baseURL}/kc/downloadAttach/${id}`
  downloadFile(url, fileName)
}

export default {
  uuid,
  queryArticleById,
  deleteArticle,
  queryArticleList,
  queryArticleListTotal,
  saveOrUpdateArticle,
  queryDict,
  queryArticleTags,
  indexAllArticle,
  indexArticleById,
  deleteAttach,
  queryAttachList,
  downloadAttach
}